//
//  GCEnemyShip.m
//  GalaxyConqueror
//
//  Created by  Stefan Nestorov on 8/16/14.
//  Copyright (c) 2014 Stefan.Nestorov. All rights reserved.
//

#import "GCEnemyShip.h"
#import "GCProjectile.h"

// pi is approximately equal to 3.14159265359.
#define   DEGREES_TO_RADIANS(degrees)  ((3.14 * degrees)/ 180)

@implementation GCEnemyShip
{
    CGFloat _screenWidth;
    CGFloat _screenHeight;
}

-(instancetype)init
{
    
    SKTextureAtlas *spritesAtlas = [GCGlobals textureAtlasNamed:@"sprites"];
    
    self = [super initWithTexture:[spritesAtlas textureNamed:@"Enemy_1"]];
    if ( self ) {
        
        [self setupPhysics];
        
        _health = 100;
        
        [self scheduleShoot];
        
        _screenWidth = [[UIScreen mainScreen] bounds].size.height;
        _screenHeight = [[UIScreen mainScreen] bounds].size.width;
        
        [self firstEnemyTrajectoryWithXPosition:_screenWidth - 150 moveToY:100 entranceDuration:2 circleRadius:75 circleActionDuration:5 andExitDuration:3];
//        [self secondEnemyTrajectoryWithStartPosition:CGPointMake(_screenWidth - 150, _screenHeight + self.size.height / 2) moveByY:-150 entranceDuration:2 waitTime:2 andExitDuration:2];
//        [self thirdEnemyTrajectoryWithStartPosition:CGPointMake(_screenWidth - 250, -self.size.height / 2) arcRadius:150 startAngle:180 endAngle:90 arcActionDuration:1.5 andExitDuration:2];
//        [self fourthEnemyTrajectoryWithYPosition:250 entranceDuration:2 arcRadius:25 startAngle:90 endAngle:270 arcActionDuration:0.5 andExitDuration:2];
//        [self fifthEnemyTrajectoryWithYPosition:200 entranceDuration:1.5 waitTime:2 andExitDuration:2];
        
    }
    
    return self;
    
}

-(void)setupPhysics
{
    
    self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.size];
    [self skt_attachDebugRectWithSize:self.size color:[SKColor redColor]];
    self.physicsBody.affectedByGravity = NO;
    self.physicsBody.dynamic = YES;
    
    self.physicsBody.categoryBitMask = ENEMY_SHIP_CATEGORY;
    self.physicsBody.collisionBitMask = 0;
    self.physicsBody.contactTestBitMask = PLAYER_SHIP_CATEGORY | PLAYER_PROJECTILE_CATEGORY;
    
}

-(void)update:(CFTimeInterval)dt
{
    
//    NSLog(@"EnemyShipPosition x: %.2f, y:%.2f",self.position.x, self.position.y);
    
    if ( _health <= 0 ) {
        _shouldBeRemoved = YES;
    }
    
}


-(void)scheduleShoot
{
    
    SKAction *wait = [SKAction waitForDuration:0.8];
    SKAction *performSelector = [SKAction performSelector:@selector(shoot) onTarget:self];
    SKAction *sequence = [SKAction sequence:@[wait, performSelector]];
    SKAction *repeat   = [SKAction repeatActionForever:sequence];
    [self runAction:repeat];
    
}


-(void)shoot
{
    
    __weak GCEnemyShip *weakSelf = self;
    GCProjectile *projectile = [[ GCProjectile alloc] initWithShootingObject:weakSelf];
    [self.delegate addProjectile:projectile];
    
}


-(SKAction *)followSquarePathWithRect:(CGRect)rect andDuration:(CGFloat)duration
{
    
    UIBezierPath *square = [UIBezierPath bezierPathWithRect:rect];
    SKAction *followSquare = [SKAction followPath:square.CGPath asOffset:YES orientToPath:NO duration:duration];
    
    return followSquare;
    
}


-(SKAction *)followCirclePathWithRect:(CGRect)rect cornerRadius:(CGFloat)radius andDuration:(CGFloat)duration
{
    
    UIBezierPath *circle = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius];
    SKAction *followCircle = [SKAction followPath:circle.CGPath asOffset:YES orientToPath:NO duration:duration];
    
    return followCircle;
}


-(void)firstEnemyTrajectoryWithXPosition:(CGFloat)xPosition moveToY:(CGFloat)yPosition entranceDuration:(CGFloat)entranceDuration circleRadius:(CGFloat)circleRadius circleActionDuration:(CGFloat)circleActionDuration andExitDuration:(CGFloat)exitDuration
{
    
    [self setPosition:CGPointMake(xPosition, self.size.height / 2)];
    
    SKAction *entranceLinePath = [SKAction moveToY:yPosition duration:entranceDuration];
    SKAction *followCircle = [self followCirclePathWithRect:CGRectMake(-circleRadius, 0, circleRadius*2, circleRadius*2) cornerRadius:circleRadius andDuration:circleActionDuration];
    SKAction *exitLinePath = [SKAction moveToX:-self.size.width / 2 duration:exitDuration];
    SKAction *removeSprite = [SKAction performSelector:@selector(removeSprite) onTarget:self];
    
    [self runAction:[SKAction sequence:@[entranceLinePath,followCircle,exitLinePath,removeSprite]]];
    
}


-(void)secondEnemyTrajectoryWithStartPosition:(CGPoint)startPosition moveByY:(CGFloat)YDistance entranceDuration:(CGFloat)entranceDuration waitTime:(CGFloat)waitTime andExitDuration:(CGFloat)exitDuration
{
    
    [self setPosition:startPosition];
    
    SKAction *entranceLinePath = [SKAction moveByX:0 y:YDistance duration:entranceDuration];
    SKAction *wait = [SKAction waitForDuration:waitTime];
    SKAction *exitLinePath = [SKAction moveToY:-self.size.height / 2 duration:exitDuration];
    SKAction *removeSprite = [SKAction performSelector:@selector(removeSprite) onTarget:self];
    
    [self runAction:[SKAction sequence:@[entranceLinePath, wait, exitLinePath,removeSprite]]];
    
}


-(void)thirdEnemyTrajectoryWithStartPosition:(CGPoint)startPosition arcRadius:(CGFloat)arcRadius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle arcActionDuration:(CGFloat)arcActionDuration andExitDuration:(CGFloat)exitDuration
{
    
    [self setPosition:startPosition];
    
    UIBezierPath *arcPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(arcRadius,0)
                                                           radius:arcRadius
                                                       startAngle:DEGREES_TO_RADIANS(startAngle)
                                                         endAngle:DEGREES_TO_RADIANS(endAngle)
                                                        clockwise:NO];
    SKAction *arcAction = [SKAction followPath:arcPath.CGPath asOffset:YES orientToPath:NO duration:arcActionDuration];
    SKAction *wait = [SKAction waitForDuration:1];
    SKAction *exitLinePath = [SKAction moveToY:_screenHeight + self.size.height/2 duration:exitDuration];
    SKAction *removeSprite = [SKAction performSelector:@selector(removeSprite) onTarget:self];
    
    [self runAction:[SKAction sequence:@[arcAction, wait, exitLinePath,removeSprite]]];
    
}


-(void)fourthEnemyTrajectoryWithYPosition:(CGFloat)yPosition entranceDuration:(CGFloat)entranceDuration arcRadius:(CGFloat)arcRadius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle arcActionDuration:(CGFloat)arcActionDuration andExitDuration:(CGFloat)exitDuration
{

    [self setPosition:CGPointMake(_screenWidth + self.size.width / 2, yPosition)];
    
    SKAction *entranceAction = [SKAction moveByX:-400 y:0 duration:entranceDuration];
    UIBezierPath *arcPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0,-arcRadius)
                                                         radius:arcRadius
                                                     startAngle:DEGREES_TO_RADIANS(startAngle)
                                                       endAngle:DEGREES_TO_RADIANS(endAngle)
                                                      clockwise:YES];
    SKAction *arcAction = [SKAction followPath:arcPath.CGPath asOffset:YES orientToPath:NO duration:arcActionDuration];
    SKAction *exitAction = [SKAction moveToX:_screenWidth + self.size.width / 2 duration:exitDuration];
    SKAction *removeSprite = [SKAction performSelector:@selector(removeSprite) onTarget:self];
    
    [self runAction:[SKAction sequence:@[entranceAction, arcAction, exitAction,removeSprite]]];
    
}


-(void)fifthEnemyTrajectoryWithYPosition:(CGFloat)yPosition entranceDuration:(CGFloat)entranceDuration waitTime:(CGFloat)waitTime andExitDuration:(CGFloat)exitDuration
{
    
    [self setPosition:CGPointMake(_screenWidth + self.size.width / 2, yPosition)];
    
    SKAction *entranceAction = [SKAction moveByX:-200 y:0 duration:entranceDuration];
    SKAction *wait = [SKAction waitForDuration:waitTime];
    SKAction *exitAction = [SKAction moveToX:- self.size.width / 2 duration:exitDuration];
    SKAction *removeSprite = [SKAction performSelector:@selector(removeSprite) onTarget:self];
    
    [self runAction:[SKAction sequence:@[entranceAction, wait, exitAction,removeSprite]]];
    
}

-(void)removeSprite
{
    
    _shouldBeRemoved = YES;
    
}



-(void)dealloc
{
    
    NSLog(@"Dealloc %@",[self class]);
    
}


@end
